<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    /**
     * 定义主键字段
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 定义表名，不含前缀
     *
     * @var string
     */
    protected $table      = 'wd_order';

    /**
     * 定义拒绝保存的字段
     *
     * @var array
     */
    protected $guarded    = ['id'];

    /**
     * 开启laravel默认的时间记录
     *
     * @var bool
     */
    public $timestamps = true;

    /**
     * 一对一
     *
     * @return void
     */
    public function user()
    {
        return $this->belongsTo('App\Models\User', 'user_id', 'id');
    }
    
    /**
     * 给订单分钱
     *
     * @param [type] $order
     * @return void
     */
    public static function create_rebate_order( Order $order, $admin_id )
    {
        $info = SystemBalance::find(1);

        $ping_fee   = $info['ping_fee'];
        $shop_fee   = $info['shop_fee'];
        $queque_fee = $info['queque_fee'];
        $fanli_fee  = $info['fanli_fee'];

        if ( !$info ) {
            $ping_fee   = config('setup.rebate.ping_fee');
            $shop_fee   = config('setup.rebate.shop_fee');
            $queque_fee = config('setup.rebate.queque_fee');
            $fanli_fee  = config('setup.rebate.fanli_fee');
        }

        $debt_money     = $order['rebate_amount'] * $queque_fee / 100;
        $rebate_money   = $order['rebate_amount'] * $fanli_fee / 100;
        $shop_money     = $order['rebate_amount'] * $shop_fee / 100;
        $platform_money = $order['rebate_amount'] * $ping_fee / 100;
        
        RebateOrder::create([
            'user_id'        => $order['user_id'],
            'shop_id'        => $order['shop_id'],
            'order_id'       => $order['id'],
            'order_sn'       => $order['order_sn'],
            'total_price'    => $order['total_amount'],
            'rebate_amount'  => $order['rebate_amount'],
            'debt_money'     => $debt_money,
            'rebate_money'   => $rebate_money,
            'shop_money'     => $shop_money,
            'platform_money' => $platform_money,
            'admin_id'       => $admin_id,
           
        ]);        
       
       return true;
    }

}
